package com.zys.el.user.controller;


import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.LongCodec;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.zys.el.core.po.ResponseBean;
import com.zys.el.user.config.po.SynthesizeResult;
import com.zys.el.user.service.ISynthesizeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/synthesize")
public class SynthesizeController {

    @Resource
    private ISynthesizeService service;
    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "加载", notes = "排名")
    @GetMapping("/list_ready/{userId}")
    public ResponseBean getSynthesize(@PathVariable Long userId) throws JsonProcessingException {
        ResponseBean rm = new ResponseBean();
        String o = (String) redisTemplate.opsForValue().get("SY" + userId);
        if (o == null) {
            SynthesizeResult synthesizeByForth = service.getSynthesizeByForth(userId);
            if (null == synthesizeByForth) {
                SynthesizeResult result = new SynthesizeResult();
                result.setReadySize(service.getSynthesizeByReady());
            } else {
                synthesizeByForth.setReadySize(service.getSynthesizeByReady());
            }
            rm.setModel(synthesizeByForth);
            redisTemplate.opsForValue().set("SY" +userId, com.alibaba.fastjson.JSON.toJSONString(synthesizeByForth));
        }
        SynthesizeResult jsonObject = JSON.parseObject(o, SynthesizeResult.class);
        rm.setModel(jsonObject);

        return rm;
    }


}
